什么是SQL?
- SQL是一种结构化查询语言(structured query language)
- 我们用它来访问数据库
others
select语句
1 2 3
| SELECT * FROM tablename SELECT columnname FROM tablename SELECT name1,name2 FROM tablename
|
INSERT INTO
1 2
| INSERT INTO tablename VALUES(v1,v2,v3) INSERT INTO tablename(c1,c2,c3) VALUES(v1,v2,v3)
|
UPDATE
1 2
| UPDATE tablename SET columnname=value WHERE columnname=value UPDATE tablename SET columnname1=value1,columnname2=value2 WHERE columnname=value
|
DELETE
1 2
| DELETE FROM tablename WHERE columname=value DELETE * FROM tablename 不删除表的情况下删除所有行
|
TOP
1
| SELECT TOP 3 * FROM tablename 返回前三条数据
|
TOP PERCENT
1
| SELECT TOP 50 PERCENT * FROM tablename 返回前50%的数据
|
LIKE
1 2 3 4 5
| SELECT * FROM tablename WHERE col LIKE 'N%' 返回col的值以N开头的记录 SELECT * FROM tablename WHERE col LIKE '%ooo%' % 代表模式中缺少的字母
SELECT * FROM tablename WHERE col NOT LIKE '%ooo%
|
IN
Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing
1 2
| SELECT * FROM Persons WHERE LastName IN ('Adams','Carter')
|
Id LastName FirstName Address City
1 Adams John Oxford Street London
3 Carter Thomas Changan Street Beijing
Alias/AS 取别名
1 2
| SELECT LastName AS Family, FirstName AS Name FROM Persons
|
Family Name
Adams John
Bush George
Carter Thomas
JOIN
1 2 3 4 5
| SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER JOIN Orders ON Persons.Id_P = Orders.Id_P ORDER BY Persons.LastName
|
UNION 连接结果集
1 2 3
| SELECT column_name(s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2
|
CREATE TABLE
1 2 3 4 5 6 7 8 9 10
| CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), UNIQUE(Id_P), PRIMARY KEY (Id_P) )
|
1 2 3 4 5 6 7 8 9
| CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), FOREION KEY (Id_P) REFFERENCES Persons(Id_P) )
|
CHECK约束
1 2 3 4 5 6 7 8
| CREATE TABLE Persons ( Id_P int NOT NULL CHECK (Id_P>0), LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )
|
欢迎与我分享你的看法。
转载请注明出处:http://taowusheng.cn/
微博:寒枫–0-0–
知乎:https://www.zhihu.com/people/tao-wu-sheng
豆瓣:YIFEI